package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.SendMailUtil;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Component;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.net.PasswordAuthentication;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Properties;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("diskTask")
public class DiskTask {


    public void checkRemainingSpace(String recipients, Integer threshold){
        diskUsageVerification(recipients, threshold);
    }


    private static void diskUsageVerification(String recipients, Integer threshold) {

        recipients = recipients.replace(" ", ",");

        // 指定要监控的磁盘路径
        String diskPath = "/";

        // 获取磁盘信息
        File disk = new File(diskPath);
        // 总空间
        long totalSpace = disk.getTotalSpace();
        // 可用空间
        long freeSpace = disk.getUsableSpace();
        // 已用空间
        long usedSpace = totalSpace - freeSpace;
        // 使用率
        double usagePercentage = (double) usedSpace / totalSpace * 100;

        if (usagePercentage > threshold) {
            String totalSpaceMsg = "总空间：" + totalSpace / 1024 / 1024 / 1024 + "G";
            String freeSpaceMsg = "已用空间：" + usedSpace / 1024 / 1024 / 1024 + "G";
            String usedSpaceMsg = "可用空间：" + freeSpace / 1024 / 1024 / 1024 + "G";
            String allMsg = "磁盘空间使用率过高: <br>" + totalSpaceMsg + "<br>" + freeSpaceMsg + "<br>" + usedSpaceMsg + "<br>" + "当前使用率为：" + new DecimalFormat("#.##").format(usagePercentage) + "%，请及时清理磁盘空间！";

            // 发送邮件提醒
            SendMailUtil.send(recipients,
                    "磁盘空间使用率过高",
                    allMsg,
                    "smtps",
                    "smtp.qq.com",
                    "3270255537@qq.com",
                    "465", "3270255537",
                    "jxidgowysjhfdbbd");
        }
    }
}
